﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PicHunter.Logging
{
    public class Log
    {
        #region Singleton
        private static Log instance_ { get; set; }
        private static Object SyncRootStatic = new Object();

        public Object SyncRoot
        {
            get
            {
                return Log.SyncRootStatic;
            }
        }

        public static Log Instance
        {
            get
            {
                lock (Log.SyncRootStatic)
                {
                    if (instance_ == null)
                    {
                        instance_ = new Log();
                    }
                }
                return instance_;
            }
        }

        private Log() 
        {
            this.Listeners = new List<ILogListener>();
        }
        #endregion

        #region Listener
        private List<ILogListener> Listeners { get; set; }

        public void AddListener(ILogListener listener)
        {
            Listeners.Add(listener);
        }

        public void RemoveListener(ILogListener listener)
        {
            Listeners.Remove(listener);
        }
#endregion

        public void Detail(Object msg)
        {
            lock (this.SyncRoot)
            {
                if (Listeners != null)
                {
                    try
                    {
                        foreach (var item in Listeners)
                        {
                            LogMessage lm = CreateLogMessage();
                            lm.Level = LogMessageLevel.DETAIL;
                            lm.Message = msg;

                            item.OnLogEvent(lm);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        public void Debug(Object msg)
        {
            lock (this.SyncRoot)
            {
                if (Listeners != null)
                {
                    try
                    {
                        foreach (var item in Listeners)
                        {
                            LogMessage lm = CreateLogMessage();
                            lm.Level = LogMessageLevel.DEBUG;
                            lm.Message = msg;

                            item.OnLogEvent(lm);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        public void Info(Object msg)
        {
            lock (this.SyncRoot)
            {
                if (Listeners != null)
                {
                    try
                    {
                        foreach (var item in Listeners)
                        {
                            LogMessage lm = CreateLogMessage();
                            lm.Level = LogMessageLevel.INFO;
                            lm.Message = msg;

                            item.OnLogEvent(lm);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }



        public void Warn(Object msg)
        {
            lock (this.SyncRoot)
            {
                if (Listeners != null)
                {
                    try
                    {
                        foreach (var item in Listeners)
                        {
                            LogMessage lm = CreateLogMessage();
                            lm.Level = LogMessageLevel.WARN;
                            lm.Message = msg;

                            item.OnLogEvent(lm);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        public void Error(Object msg)
        {
            lock (this.SyncRoot)
            {
                if (Listeners != null)
                {
                    try
                    {
                        foreach (var item in Listeners)
                        {
                            LogMessage lm = CreateLogMessage();
                            lm.Level = LogMessageLevel.ERROR;
                            lm.Message = msg;

                            item.OnLogEvent(lm);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }


        LogMessage CreateLogMessage()
        {
            LogMessage lm = new LogMessage();
            lm.Date = DateTime.Now;
            lm.ThreadId = Thread.CurrentThread.ManagedThreadId;

            return lm;
        }






    }
}

